package com.jred.jforum.service;

import java.util.List;
import java.util.Map;

import com.jred.jforum.beans.forum.Forum;
import com.jred.jforum.beans.forum.Group;
import com.jred.jforum.beans.forum.Message;
import com.jred.jforum.beans.forum.Tag;
import com.jred.jforum.beans.forum.Topic;
import com.jred.jforum.dao.sqlmap.ForumDAO;

public class ForumService {
	
	private ForumDAO forumDAO = null;

    public void setForumDAO(ForumDAO forumDAO) {
            this.forumDAO = forumDAO;
    }
    
    public List getGroups(){
    	return getGroups(1,true);
    }
    
	public List getGroups(int userStatus, boolean filled){
        List list = forumDAO.getGroups(userStatus, filled);
        for(int i=0;i<list.size();i++){
        	Group g = (Group)list.get(i);
        	getForums(g,userStatus, filled);
        }
        return list;
	}
	public void getForums(Group g,int userStatus, boolean filled){
		List list = forumDAO.getForums(g.getGroupid(), true);
		
		for(int i = 0; i<list.size();i++){
			Forum forum = (Forum)list.get(i);
			if (filled) {
	            forum.setThreadsCount(forumDAO.countForumTopics(forum.getForumid()).intValue());
	            forum.setTagList(forumDAO.getForumTags(forum.getForumid(), true));
	            Message m = forumDAO.getLastMessageByForumid(forum.getForumid());
	            if(m!=null){
		            forum.setLastMessThreadId(m.getThreadid());
		            forum.setLastMessage(m);
	            }
	        }
			g.addForum(forum);
		}

	}	

	
	public List getTopicsByForumid(int forumid){
		List list = forumDAO.getTopics(forumid, true);
		for(int i = 0; i<list.size();i++){
			Topic t = (Topic)list.get(i);
			t.setLastMessage(forumDAO.getLastMessageByThreadid(t.getThreadid()));
		}
		return list;
	}
	
	public List getTopicsByTagid(Map map){
		List list = forumDAO.getTopicsByTagid(map, true);
		for(int i = 0; i<list.size();i++){
			Topic t = (Topic)list.get(i);
			t.setLastMessage(forumDAO.getLastMessageByThreadid(t.getThreadid()));
		}
		return list;
	}
	
	public Forum getForumInfo(int forumid){
		return forumDAO.getForumById(forumid, true);
	}

	public Topic getThreadInfo(int threadid) {
		// TODO Auto-generated method stub
		return forumDAO.getThreadById(threadid, true);
	}
	
	public Message getMessageInfo(int messageid) {
		// TODO Auto-generated method stub
		return forumDAO.getMessageInfo(messageid);
	}
	
	public List getForumTags(int forumid) {
		// TODO Auto-generated method stub
		return forumDAO.getForumTags(forumid, true);
	}
	
	public void updateThreadVcount(int threadid) {
		forumDAO.updateThreadVcount(threadid);
	}
	
	public List getThreadMessages(int threadid){
		return forumDAO.getThreadMessages(threadid);
	}	
	
	public int getNumReplys(int threadid){
		return (Integer)forumDAO.getNumReplys(threadid).intValue();
	}
	
	public Map getSystemConstants(){
		return forumDAO.getSystemConstants();
	}
	
	public int addMessage(Message msg){
		return forumDAO.addMessage(msg);
	}
	
}
